/**
 * @fileoverview markdown 插件
 * Include marked (https://github.com/markedjs/marked)
 * Include github-markdown-css (https://github.com/sindresorhus/github-markdown-css)
 */
import marked from './marked.min'
let index = 0

function Markdown (vm) {
  this.vm = vm
  vm._ids = {}
}

Markdown.prototype.onUpdate = function (content) {
  if (this.vm.markdown) {
	  let that = this;
	  // 自定义渲染方式
	  var rendererMD = new marked.Renderer();
	  // 对段落中==xxx==的标记的内容替换成<mark>xxx</mark>
	  rendererMD.paragraph = function(text) {
	  	if(text) return '<p>' + text.replace(/==(.*?)==/g,"<mark>$1</mark>").replace(/::: hljs-center(.*?):::/g, '<p style="text-align: center;text-indent: 0em;">$1</p>')+ '</p>';
	  	else '<p></p>'
	  }
    return marked(content, { renderer: rendererMD })
  }
}

Markdown.prototype.onParse = function (node, vm) {
  if (vm.options.markdown) {
    // 中文 id 需要转换，否则无法跳转
    if (vm.options.useAnchor && node.attrs && /[\u4e00-\u9fa5]/.test(node.attrs.id)) {
      const id = 't' + index++
      this.vm._ids[node.attrs.id] = id
      node.attrs.id = id
    }
    if (node.name === 'p' || node.name === 'table' || node.name === 'tr' || node.name === 'th' || node.name === 'td' || node.name === 'blockquote' || node.name === 'pre' || node.name === 'code') {
      node.attrs.class = `md-${node.name} ${node.attrs.class || ''}`
    }
  }
}

export default Markdown
